﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FTools.Dominio;
using System.Data.SqlClient;
using System.Data;

namespace FTools.Persistencia
{
    class PerSpStock
    {
        static string msg = "";

        //OBTENER SpStock
        public SpStock obtenerSpStock(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdSubProducto)
        {
            SpStock objSpStock = new SpStock();
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select * from SP_STOCK where ID_SUB_PRODUCTO=@IdSubProducto");

                command.Parameters.AddWithValue("@IdSubProducto", xIdSubProducto);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];

                    objSpStock.IdSubProducto = (long)fila.ItemArray[0];
                    objSpStock.StockMin = Convert.ToDouble(fila.ItemArray[1]);
                    objSpStock.Stock = Convert.ToDouble(fila.ItemArray[2]);
                }
                return objSpStock;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //EXISTE SP_STOCK
        public Boolean existeSpStock(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdSubProducto)
        {
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select count(*) as EXISTE from SP_STOCK where ID_SUB_PRODUCTO=@IdSubProducto");

                command.Parameters.AddWithValue("@IdSubProducto", xIdSubProducto);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    if ((int)fila.ItemArray[0] > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //ALTA SP_STOCK
        public int altaSpStock(SqlConnection xConexion, SqlTransaction xTransaccion, SpStock xObjSpStock)
        {
            try
            {
                SqlCommand command = new SqlCommand("insert into SP_STOCK values (@IdSubProducto, @StockMin, @Stock)");

                command.Parameters.AddWithValue("@IdSubProducto", xObjSpStock.IdSubProducto);
                command.Parameters.AddWithValue("@StockMin", xObjSpStock.StockMin);
                command.Parameters.AddWithValue("@Stock", xObjSpStock.Stock);

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //BAJA SP_STOCK
        public int bajaSpStock(SqlConnection xConexion, SqlTransaction xTransaccion, SpStock xObjSpStock)
        {
            try
            {
                SqlCommand command = new SqlCommand("delete from SP_STOCK where ID_SUB_PRODUCTO=@IdSubProducto");

                command.Parameters.AddWithValue("@IdSubProducto", xObjSpStock.IdSubProducto);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //MODIFICA SP_STOCK
        public int modificaSpStock(SqlConnection xConexion, SqlTransaction xTransaccion, SpStock xObjSpStock)
        {
            try
            {
                SqlCommand command = new SqlCommand("update SP_STOCK set STOCK_MIN=@StockMin, STOCK=@Stock " +
                                                    "where ID_SUB_PRODUCTO=@IdSubProducto");

                command.Parameters.AddWithValue("@IdSubProducto", xObjSpStock.IdSubProducto);
                command.Parameters.AddWithValue("@StockMin", xObjSpStock.StockMin);
                command.Parameters.AddWithValue("@Stock", xObjSpStock.Stock);

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }
    }
}
